﻿
#include "zyButtonEx.h"

DarkMoonUI::zyButtonEx::zyButtonEx()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyButtonEx_WndProc);
	m_obj->setChildWndProc(zyButtonEx_ChildWndProc);
	m_obj->setDestroy(zyButtonEx_Destroy);
	
}
DarkMoonUI::zyButtonEx::~zyButtonEx()
{

}
void DarkMoonUI::zyButtonEx::CalculateLayout(const Gdiplus::RectF& rcSrc,TextImageRelation relation, float imgWidth, float imgHeight,float textWidth,
	float textHeight,float minTextWidth,float minTextHeight, Gdiplus::RectF& desImgRect, Gdiplus::RectF& desTextRect) {
	const float totalWidth = static_cast<float>(rcSrc.GetRight() - rcSrc.GetLeft());
	const float totalHeight = static_cast<float>(rcSrc.GetBottom() - rcSrc.Y);

	switch (relation) {
	case ImageOverText: {
		desImgRect = { rcSrc.GetLeft(), rcSrc.Y, totalWidth, totalHeight };
		desTextRect = { rcSrc.GetLeft(), rcSrc.Y, totalWidth, totalHeight };
		break;
	}

	case ImageBeforeText: // 图左文右
	case ImageAfterText: { // 图右文左
		const bool isImageFirst = (relation == ImageBeforeText);
		const float contentWidth = imgWidth + textWidth;

		if (contentWidth > totalWidth) {
			const float availableWidth = totalWidth - imgWidth;
			const float textWidthActual = availableWidth > minTextWidth ? availableWidth : minTextWidth;

			if (isImageFirst) {
				desImgRect = { rcSrc.GetLeft(), rcSrc.Y, imgWidth, totalHeight };
				desTextRect = { rcSrc.GetLeft() + imgWidth, rcSrc.Y, textWidthActual, totalHeight };
			}
			else {
				desTextRect = { rcSrc.GetLeft(), rcSrc.Y, textWidthActual, totalHeight };
				desImgRect = { rcSrc.GetLeft() + textWidthActual, rcSrc.Y, imgWidth, totalHeight };
			}
		}
		else {
			const float ratio = isImageFirst ?
				(imgWidth / contentWidth) :
				(textWidth / contentWidth);
			const float firstPart = totalWidth * ratio;

			if (isImageFirst) {
				desImgRect = { rcSrc.GetLeft(), rcSrc.Y, firstPart, totalHeight };
				desTextRect = { rcSrc.GetLeft() + firstPart, rcSrc.Y, totalWidth - firstPart, totalHeight };
			}
			else {
				desTextRect = { rcSrc.GetLeft(), rcSrc.Y, firstPart, totalHeight };
				desImgRect = { rcSrc.GetLeft() + firstPart, rcSrc.Y, totalWidth - firstPart, totalHeight };
			}
		}
		break;
	}

	case ImageAboveText: // 图上文下
	case ImageBelowText: { // 图下文上
		const bool isImageFirst = (relation == ImageAboveText);
		const float contentHeight = imgHeight + textHeight;

		if (contentHeight > totalHeight) {
			const float availableHeight = totalHeight - imgHeight;
			const float textHeightActual = availableHeight > minTextHeight ? availableHeight : minTextHeight;

			if (isImageFirst) {
				desImgRect = { rcSrc.GetLeft(), rcSrc.Y, totalWidth, imgHeight };
				desTextRect = { rcSrc.GetLeft(), rcSrc.Y + imgHeight, totalWidth, textHeightActual };
			}
			else {
				desTextRect = { rcSrc.GetLeft(), rcSrc.Y, totalWidth, textHeightActual };
				desImgRect = { rcSrc.GetLeft(), rcSrc.Y + textHeightActual, totalWidth, imgHeight };
			}
		}
		else {
			const float ratio = isImageFirst ?
				(imgHeight / contentHeight) :
				(textHeight / contentHeight);
			const float firstPart = totalHeight * ratio;

			if (isImageFirst) {
				desImgRect = { rcSrc.GetLeft(), rcSrc.Y, totalWidth, firstPart };
				desTextRect = { rcSrc.GetLeft(), rcSrc.Y + firstPart, totalWidth, totalHeight - firstPart };
			}
			else {
				desTextRect = { rcSrc.GetLeft(), rcSrc.Y, totalWidth, firstPart };
				desImgRect = { rcSrc.GetLeft(), rcSrc.Y + firstPart, totalWidth, totalHeight - firstPart};
			}
		}
		break;
	}

	default:
		// 处理未知类型或添加默认行为
		break;
	}
}
void DarkMoonUI::zyButtonEx::zyButtonEx_DrawIconAndText(HWND hWnd, Gdiplus::Graphics* graphics, int state, RECT rcItem) {

	int AlignIcon = 0, IconMarginLeft = 0, IconMarginTop = 0, IconMarginRight = 0, IconMarginBottom = 0 ;

	int IconWidth = 0;
	int IconHeight = 0;
	Gdiplus::Image* pIconImage = (Gdiplus::Image*)GetMember(L"pIconImage");
	if (pIconImage) {
		AlignIcon = (int)GetMember(L"AlignIcon");
		IconMarginLeft = (int)GetMember(L"IconMarginLeft");
		IconMarginTop = (int)GetMember(L"IconMarginTop");
		IconMarginRight = (int)GetMember(L"IconMarginRight");
		IconMarginBottom = (int)GetMember(L"IconMarginBottom");
		if (HasMember(L"IconWidth") ){
			IconWidth = (int)GetMember(L"IconWidth");
		}
		else
		{
			IconWidth = pIconImage->GetWidth();
		}
		if (HasMember(L"IconHeight")) {
			IconHeight = (int)GetMember(L"IconHeight");
		}
		else
		{
			IconHeight = pIconImage->GetHeight();
		}
	}
	RectF retRect , minRect;
	RectF IconRect, TextRect;
	RectF rectDes;
	
	zyButtonEx *pButtonEx = new zyButtonEx();
	pButtonEx->setObject(getObject());
	std::wstring text = pButtonEx->取文本();
	int length = (int)text.size();
	int TextMarginLeft = 0, TextMarginTop = 0;


	HFONT hFont = (HFONT)SendMessage(hWnd, WM_GETFONT, 0, 0);
	if (!hFont) return;
	HDC hdc1 = GetDC(hWnd);
	if (!hdc1) return;
	Gdiplus::Font 局部_字体(hdc1, hFont);
	ReleaseDC(hWnd, hdc1);

	Gdiplus::SolidBrush brush(Color(255, 0, 0, 255));

	Gdiplus::StringFormat stringFormat(StringFormatFlagsMeasureTrailingSpaces);
	if(length > 0)
	{
		TextMarginLeft = (int)GetMember(L"TextMarginLeft");
		TextMarginTop = (int)GetMember(L"TextMarginTop");

		int AlignText = (int)GetMember(L"AlignText");
		
		if (HasMember( L"TextColor"))
			brush.SetColor((ARGB)GetMember(L"TextColor"));

		SetTextAlignment(stringFormat, AlignText);
		stringFormat.SetHotkeyPrefix(HotkeyPrefixShow);
		TextRect = { (REAL)(rcItem.left), (REAL)(rcItem.top), (REAL)(rcItem.right - rcItem.left), (REAL)(rcItem.bottom - rcItem.top) };
		graphics->MeasureString(text.substr(0,1).c_str(), 1, &局部_字体, TextRect, &stringFormat, &minRect);
		if (pIconImage) {
			rectDes = { (REAL)(rcItem.left + 4), (REAL)(rcItem.top + 4), (REAL)(rcItem.right - 4), (REAL)(rcItem.bottom - 4) };
			CalculateLayout(rectDes, (TextImageRelation)GetMember(L"TextImageRelation"), (REAL)IconWidth, (REAL)IconHeight,
				0, 0, minRect.Width, minRect.Height, IconRect, TextRect);
		}
		else
		{
			TextRect = { (REAL)(rcItem.left), (REAL)(rcItem.top), (REAL)(rcItem.right - rcItem.left), (REAL)(rcItem.bottom - rcItem.top) };
		}
		graphics->MeasureString(text.c_str(), -1, &局部_字体, TextRect, &stringFormat, &retRect);
	}
	rectDes = { (REAL)(rcItem.left + 4), (REAL)(rcItem.top + 4), (REAL)(rcItem.right - 4), (REAL)(rcItem.bottom - 4) };
	CalculateLayout(rectDes, (TextImageRelation)GetMember(L"TextImageRelation"), (REAL)IconWidth, (REAL)IconHeight,
		retRect.Width, retRect.Height, minRect.Width, minRect.Height, IconRect, TextRect);
	
	TextRect.X = TextRect.X + TextMarginLeft;
	TextRect.Y = TextRect.Y + TextMarginTop;
	if(pIconImage)
		IconRect = GetAlignRect(IconRect, (Align)AlignIcon, (REAL)IconWidth, (REAL)IconHeight, (REAL)IconMarginLeft, (REAL)IconMarginTop, (REAL)IconMarginRight, (REAL)IconMarginBottom);
	if ((state & ODS_DISABLED) != 0)
	{

		brush.SetColor(-8421504);
	}
	else if((state & ODS_FOCUS) == ODS_FOCUS)//焦点
	{
		if (HasMember(L"TextFocusColor")) {
			brush.SetColor((ARGB)GetMember(L"TextFocusColor"));
		}
		if ((state & zyButtonState_Hover) != 0) {//悬停
			if (HasMember(L"TextHoverColor")) {
				brush.SetColor((ARGB)GetMember(L"TextHoverColor"));
			}
		}
	}
	else {
		if ((state & zyButtonState_Hover) != 0) {//悬停
			if (HasMember(L"TextHoverColor")) {
				brush.SetColor((ARGB)GetMember(L"TextHoverColor"));
			}
		}
	}

	if (pIconImage)
		graphics->DrawImage(pIconImage, IconRect.X, IconRect.Y , (REAL)IconWidth, (REAL)IconHeight);
	// 绘制文本
	if (length > 0) {
		Gdiplus::Status lastResult = graphics->DrawString(text.c_str(), -1, &局部_字体, TextRect, &stringFormat, &brush);
		if (lastResult != Gdiplus::Ok) {
			return;
		}
	}
	delete pButtonEx;
}
RectF DarkMoonUI::zyButtonEx::GetAlignRect(const RectF& srcRect,Align align,float width,float height,float marginLeft,float marginTop,
	float marginRight,float marginBottom
) {
	RectF rc ;

	switch (align) {
	case AlignTopLeft:
		rc = { srcRect.X + marginLeft, srcRect.Y + marginTop, width, height };
		break;

	case AlignTopCenter:
		rc = {
			srcRect.X + std::round((srcRect.Width + marginLeft - marginRight - width) / 2.0f),
			srcRect.Y + marginTop,
			width, height };
		break;

	case AlignTopRight:
		rc = {
			srcRect.X + srcRect.Width - width - marginRight,
			srcRect.Y + marginTop,
			width, height };
		break;

	case AlignMiddleLeft:
		rc = {
			srcRect.X + marginLeft,
			srcRect.Y + std::round((srcRect.Height + marginTop - marginBottom - height) / 2.0f),
			width, height };
		break;

	case AlignMiddleCenter:
		rc = {
			srcRect.X + std::round((srcRect.Width + marginLeft - marginRight - width) / 2.0f),
			srcRect.Y + std::round((srcRect.Height + marginTop - marginBottom - height) / 2.0f),
			width, height };
		break;

	case AlignMiddleRight:
		rc = {
			srcRect.X + srcRect.Width - width - marginRight,
			srcRect.Y + std::round((srcRect.Height + marginTop - marginBottom - height) / 2.0f),
			width, height };
		break;

	case AlignBottomLeft:
		rc = {
			srcRect.X + marginLeft,
			srcRect.Y + srcRect.Height - height - marginBottom,
			width, height };
		break;

	case AlignBottomCenter:
		rc = {
			srcRect.X + std::round((srcRect.Width + marginLeft - marginRight - width) / 2.0f),
			srcRect.Y + srcRect.Height - height - marginBottom,
			width, height };
		break;

	case AlignBottomRight:
		rc = {
			srcRect.X + srcRect.Width - width - marginRight,
			srcRect.Y + srcRect.Height - height - marginBottom,
			width, height };
		break;

	case AlignFill:
		rc = {
			srcRect.X + marginLeft,
			srcRect.Y + marginTop,
			srcRect.Width - marginLeft - marginRight,
			srcRect.Height - marginTop - marginBottom };
		break;

	default:
		rc = { srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height };
		break;
	}

	return rc;
}
// 设置文本对齐方式的辅助函数
void DarkMoonUI::zyButtonEx::SetTextAlignment(Gdiplus::StringFormat& stringFormat, LONG_PTR align) {
	switch (align) {
	case 7: stringFormat.SetAlignment(StringAlignmentNear); stringFormat.SetLineAlignment(StringAlignmentNear); break; // 左对齐
	case 8: stringFormat.SetAlignment(StringAlignmentCenter); stringFormat.SetLineAlignment(StringAlignmentNear); break; // 水平居中，顶部对齐
	case 9: stringFormat.SetAlignment(StringAlignmentFar); stringFormat.SetLineAlignment(StringAlignmentNear); break; // 右对齐
	case 4: stringFormat.SetAlignment(StringAlignmentNear); stringFormat.SetLineAlignment(StringAlignmentCenter); break; // 左对齐，垂直居中
	case 5: stringFormat.SetAlignment(StringAlignmentCenter); stringFormat.SetLineAlignment(StringAlignmentCenter); break; // 水平和垂直居中
	case 6: stringFormat.SetAlignment(StringAlignmentFar); stringFormat.SetLineAlignment(StringAlignmentCenter); break; // 右对齐，垂直居中
	case 1: stringFormat.SetAlignment(StringAlignmentNear); stringFormat.SetLineAlignment(StringAlignmentFar); break; // 左对齐，底部对齐
	case 2: stringFormat.SetAlignment(StringAlignmentCenter); stringFormat.SetLineAlignment(StringAlignmentFar); break; // 水平居中，底部对齐
	case 3: stringFormat.SetAlignment(StringAlignmentFar); stringFormat.SetLineAlignment(StringAlignmentFar); break; // 右对齐，底部对齐
	default: stringFormat.SetAlignment(StringAlignmentNear); stringFormat.SetLineAlignment(StringAlignmentNear); break; // 默认左对齐
	}
}
void DarkMoonUI::zyButtonEx::zyButtonEx_DrawImage(HWND hWnd, HDC hdc, Gdiplus::Graphics* g, HTHEME htheme, int state, RECT rcItem) {
	int width = rcItem.right - rcItem.left;
	int height = rcItem.bottom - rcItem.top;
	bool IsNineDraw = GetMember(L"IsNineDraw");
	int nineLeft = (int)GetMember(L"NineLeft");
	int nineTop = (int)GetMember(L"NineTop");
	int nineRight = (int)GetMember(L"NineRight");
	int nineBottom = (int)GetMember(L"NineBottom");
	int bps = PBS_NORMAL;
	Gdiplus::Image* pImage = nullptr;
	ARGB TransColor = 0;
	if ((state & ODS_DISABLED) == ODS_DISABLED) {//禁止
		pImage = (Gdiplus::Image*)GetMember(L"pImageDisabled");
		TransColor = (ARGB)GetMember(L"TransColorDisabled");
		if(htheme != 0)
			bps = PBS_DISABLED;
		else 
			bps = DFCS_INACTIVE;
		
	}
	else if ((state & ODS_SELECTED) == ODS_SELECTED) //按下
	{
		pImage = (Gdiplus::Image*)GetMember(L"pImagePress");
		TransColor = (ARGB)GetMember(L"TransColorPress");
		if (htheme != 0)
			bps = PBS_PRESSED;
		else
			bps = DFCS_PUSHED;
	}
	else if ((state & ODS_SELECTED) == ODS_SELECTED) //焦点
	{
		pImage = (Gdiplus::Image*)GetMember(L"pImageDefault");
		TransColor = (ARGB)GetMember(L"TransColorDefault");
		if (htheme != 0)
			bps = PBS_DEFAULTED;
		else
			bps = 0;
        if ((state & zyButtonState_Hover) !=0) {//悬停
			pImage = (Gdiplus::Image*)GetMember(L"pImageHover");
			TransColor = (ARGB)GetMember(L"TransColorHover");
			if (htheme != 0)
				bps = PBS_HOT;
			else
				bps = DFCS_HOT;
        }
	}
	else {
		if (GetMember(L"Default") != 0) {
			pImage = (Gdiplus::Image*)GetMember(L"pImageDefault");
			TransColor = (ARGB)GetMember(L"TransColorDefault");
			if (htheme != 0)
				bps = PBS_DEFAULTED;
			else
				bps = 0;
		}
		else {
			pImage = (Gdiplus::Image*)GetMember(L"pImageNormal");//正常
			TransColor = (ARGB)GetMember(L"TransColorNormal");
			if (htheme != 0)
				bps = PBS_NORMAL;
			else
				bps = 0;
		}
		if ((state & zyButtonState_Hover) != 0) {//悬停
			pImage = (Gdiplus::Image*)GetMember(L"pImageHover");
			TransColor = (ARGB)GetMember(L"TransColorHover");
			if (htheme != 0)
				bps = PBS_HOT;
			else
				bps = DFCS_HOT;
		}
	}

	if (pImage) {
		if (IsNineDraw) {
			// 九宫格绘制使用克隆图像
			DrawNineGridImage(g, pImage, 0, 0, (REAL)width, (REAL)height, nineLeft, nineRight, nineTop, nineBottom);

		}
		else {
			Gdiplus::Status lastResult = g->DrawImage(pImage, 0, 0, width, height);
			if (lastResult != Gdiplus::Ok) {
				return;
			}
		}
		
		HRGN hRgn = 0;
		if ((state & ODS_FOCUS) == ODS_FOCUS && GetMember(L"DrawFocus") != 0) {  // 焦点
			RECT rectFocus = { rcItem.left + 3, rcItem.top + 3, rcItem.right - 4, rcItem.bottom - 4 };
			DrawFocusRect(hdc, &rectFocus);
		}
		if (hRgn != 0) {
			SetWindowRgn(hWnd, hRgn, true);
            DeleteObject(hRgn);
		}
	}
	else if (TransColor != 0) {
		Gdiplus::SolidBrush brush(TransColor);

		g->SetCompositingQuality(CompositingQualityHighQuality);
		g->SetSmoothingMode(SmoothingModeAntiAlias);
		int Rectangle = (int)GetMember(L"Rectangle");
		DrawRoundedRectangle(g, nullptr ,&brush, 0, 0, (float)width, (float)height, Rectangle);
		//Gdiplus::Status lastResult = g->FillRectangle(&brush, 0, 0, width, height);
		//std::wcout << lastResult <<"   TransColor:  " << TransColor << std::endl;
		//if (lastResult != Gdiplus::Ok) {
		//	return;
		//}
	}
	else {
		if (htheme != 0)
			DrawThemeBackground(htheme, hdc, BP_PUSHBUTTON, bps, &rcItem, 0);
		else
			DrawFrameControl(hdc, &rcItem, DFC_BUTTON, DFCS_BUTTONPUSH | bps);
		if ((state & ODS_FOCUS) == ODS_FOCUS) {  // 焦点
			RECT rectFocus = { rcItem.left + 3, rcItem.top + 3, rcItem.right - 4, rcItem.bottom - 4 };
			DrawFocusRect(hdc, &rectFocus);
		}
	}

}
void DarkMoonUI::zyButtonEx::zyButtonEx_DrawBack(HWND hWnd, Gdiplus::Graphics* g, RECT rcItem) {
	if (HasMember(L"BackColor")) {
		Gdiplus::ARGB backColor = (ARGB)GetMember(L"BackColor");
		g->Clear(backColor);
	}
	else
	{
		int backColor = (int)GetSysColor(COLOR_BTNFACE);
		g->Clear(zyRGB2ARGB(backColor,255));
	}
}
void DarkMoonUI::zyButtonEx::zyButtonEx_DrawButton(HWND hWnd, HDC hdc, int state,RECT rcItem){
	zyBuffGraphics* buff = new zyBuffGraphics();
	int w = rcItem.right - rcItem.left;
	int h = rcItem.bottom - rcItem.top;
	if(!buff->Create(w, h)) return;
	Gdiplus::Graphics* g = buff->GetGraphics();
	g->SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor);
	g->SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
	g->SetSmoothingMode(Gdiplus::SmoothingModeHighSpeed);
	zyButtonEx_DrawBack(hWnd, g, rcItem);
	HTHEME htheme = OpenThemeData(hWnd, L"Button");
	// 使用 HDC 进行绘制
	//HDC hdcGraphics = g->GetHDC();
	zyButtonEx_DrawImage(hWnd, hdc, g, htheme, state, rcItem);
	//g->ReleaseHDC(hdcGraphics);


	zyButtonEx_DrawIconAndText(hWnd, g, state, rcItem);

	if (htheme != 0)
		CloseThemeData(htheme);
	buff->Copy2dc(hdc, 0, 0, w, h, 0, 0, SRCCOPY);
	delete buff;
}
void DarkMoonUI::zyButtonEx::zyButtonEx_OnDrawItem(HWND hWnd, WPARAM wParam, DRAWITEMSTRUCT* dis) {
	int m_state = (int)GetMember(L"State");
	m_state |= dis->itemState;
	zyButtonEx_DrawButton(hWnd, dis->hDC, m_state, dis->rcItem);
}
LRESULT WINAPI DarkMoonUI::zyButtonEx::zyButtonEx_ChildWndProc(zyObject* obj, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL* bHandle)
{
	if (message == WM_DRAWITEM)
	{
		DRAWITEMSTRUCT* dis = (DRAWITEMSTRUCT*)lParam;
		if(dis->CtlType != ODT_BUTTON) return 0;
		zyButtonEx* zyb = (zyButtonEx*)obj->getPtrData(L"this");
		zyb->zyButtonEx_OnDrawItem(dis->hwndItem, wParam, dis);
		*bHandle = true;
		return 1;
	}
	else if (message == WM_COMMAND)
	{
		short id = LOWORD(wParam);// & 65535); //LOWORD
		int code = HIWORD(wParam);
		if (code == BN_CLICKED) {
			TRegEvent* evtPtr = (TRegEvent*)obj->getRegEvent(L"OnClick");
			if (evtPtr != nullptr) {
				evtPtr->handler<LRESULT>(lParam ,id);
				*bHandle = true;
				return 0;
			}
		}
	}
	return zyCtrol::zyCtrol_ChildWndProc(obj, hWnd, message, wParam, lParam, bHandle);
}
LRESULT WINAPI DarkMoonUI::zyButtonEx::zyButtonEx_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandle)
{
	if (uMsg == WM_NCDESTROY) {
		zyButtonEx* zyb = (zyButtonEx*)obj->getPtrData(L"this");
		zyb->OnNCDestroy();
	}
	else if (uMsg == WM_MOUSEMOVE) {
		zyButtonEx* zyb = (zyButtonEx*)obj->getPtrData(L"this");
		zyb->zyButtonEx_OnMouseMove(hWnd);
	}
	else if (uMsg == WM_MOUSELEAVE) {
		zyButtonEx* zyb = (zyButtonEx*)obj->getPtrData(L"this");
		zyb->zyButtonEx_OnMouseLeave(hWnd);
	}
	else if (uMsg == WM_ERASEBKGND) {
		*bHandle = true;
		return 1;
	}
	else if (uMsg == WM_KEYDOWN) {
		if (wParam == VK_RETURN)//回车键
			SendMessage(hWnd, BM_CLICK, 0, 0);
	}
	return zyCtrol::zyCtrol_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandle);
}
void DarkMoonUI::zyButtonEx::zyButtonEx_OnMouseMove(HWND hWnd){
	if (GetMember(L"isMoveIn") == 0) {
		int m_state = (int)GetMember(L"State");
		m_state |= zyButtonState_Hover;
		SetMember(L"State", m_state);
		InvalidateRect(hWnd, 0, true);
	}
}
void DarkMoonUI::zyButtonEx::zyButtonEx_OnMouseLeave(HWND hWnd) {
	int m_state = (int)GetMember(L"State");
	m_state &= ~zyButtonState_Hover;
	SetMember(L"State", m_state);
	InvalidateRect(hWnd, 0, true);
}
void DarkMoonUI::zyButtonEx::OnNCDestroy() {

	resObject* pImageDisabled = (resObject*)GetMember(L"pImageDisabled");
	if (pImageDisabled != nullptr) {
		pImageDisabled->release();
		SetMember(L"pImageDisabled", 0);
	}
	resObject* pImagePress = (resObject*)GetMember(L"pImagePress");
	if (pImagePress != nullptr) {
		pImagePress->release();
		SetMember(L"pImagePress", 0);
	}
	resObject* pImageDefault = (resObject*)GetMember(L"pImageDefault");
	if (pImageDefault != nullptr) {
		pImageDefault->release();
		SetMember(L"pImageDefault", 0);
	}
	resObject* pImageHover = (resObject*)GetMember(L"pImageHover");
	if (pImageHover != nullptr) {
		pImageHover->release();
		SetMember(L"pImageHover", 0);
	}
	resObject* pImageNormal = (resObject*)GetMember(L"pImageNormal");
	if (pImageNormal != nullptr) {
		pImageNormal->release();
		SetMember(L"pImageNormal", 0);
	}
	resObject* pIconImage = (resObject*)GetMember(L"pIconImage");
	if (pIconImage != nullptr) {
		pIconImage->release();
		SetMember(L"pIconImage", 0);
	}
}
void WINAPI DarkMoonUI::zyButtonEx::zyButtonEx_Destroy(zyObject* obj)
{
	zyButtonEx* zyb = (zyButtonEx*)obj->getPtrData(L"this");
	zyb->OnNCDestroy();
	zyCtrol::zyCtrol_Destroy(obj);
}
BOOL DarkMoonUI::zyButtonEx::创建(HWND 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	SetMember(L"EnlableHover", 0);//响应鼠标悬停事件
	SetMember(L"AlignText", 5);
	SetMember(L"AlignIcon", 4);
	SetMember(L"Rectangle", 0);
	添加ZY窗口风格(ZY窗口风格_需要回车键);
	if (参数_窗口风格 == 0)
		参数_窗口风格 =  WS_TABSTOP ;
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	if(zyWindow_SuperClass(L"Button", L"zyButtonEx") == false)
		return false;

	return _CreateWindow(参数_父窗口, 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格 | BS_OWNERDRAW | WS_CLIPSIBLINGS,
		参数_扩展风格, L"zyButtonEx");
}
BOOL DarkMoonUI::zyButtonEx::创建(zyWindow& 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	
	SetMember(L"EnlableHover", 0);//响应鼠标悬停事件
	SetMember(L"AlignText", 5);
	SetMember(L"AlignIcon", 5);
	SetMember(L"Rectangle", 0);
	添加ZY窗口风格(ZY窗口风格_需要回车键);
	if (参数_窗口风格 == 0)
		参数_窗口风格 =   WS_TABSTOP;
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	if (zyWindow_SuperClass(L"Button", L"zyButtonEx") == false)
		return false;
	return _CreateWindow(参数_父窗口.getHwnd(), 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格 | BS_OWNERDRAW | WS_CLIPSIBLINGS , 参数_扩展风格, L"zyButtonEx");
}
int DarkMoonUI::zyButtonEx::圆角度(int 参数_圆角度) {
	if (参数_圆角度 == 0) {
		return (int)GetMember(L"Rectangle");
	}
	else
	{
		SetMember(L"Rectangle", (LONG_PTR)参数_圆角度);
		return 参数_圆角度;
	}
}
bool DarkMoonUI::zyButtonEx::绑定(HWND 参数_父窗口, int 参数_资源ID, bool 参数_使用默认字体) {//成功返回真，失败返回假。
	HWND hwnd = GetDlgItem(参数_父窗口, 参数_资源ID);
	if (hwnd != 0 && 参数_使用默认字体) {
		SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
	}
	return _BindWindow(hwnd);
}
bool DarkMoonUI::zyButtonEx::绑定窗口句柄(HWND 参数_窗口句柄) {//成功返回真，失败返回假。
	if (_BindWindow(参数_窗口句柄)) {
		AddStyle(BS_OWNERDRAW | WS_CLIPSIBLINGS);
		return true;
	}
	return false;
}
std::wstring DarkMoonUI::zyButtonEx::标题(const std::wstring& 参数_标题) {
	if (!参数_标题.empty()) {
		return 取文本();
	}
	置文本(参数_标题);
	重画();
	return (L"");
}
int DarkMoonUI::zyButtonEx::背景色(int 参数_背景色 /*RGB*/) {
	int color = (int)GetMember(L"BackColor");
	if (参数_背景色 != 0) {
		SetMember(L"BackColor", 参数_背景色);
		重画();
	}
	return (color);
}
int DarkMoonUI::zyButtonEx::文本色(int 参数_背景色 /*RGB*/) {
	int color = (int)GetMember(L"TextColor");
	if (参数_背景色 != 0) {
		SetMember(L"TextColor", 参数_背景色);
		重画();
	}
	return (color);
}
bool DarkMoonUI::zyButtonEx::通常图片(Gdiplus::Image* 参数_图片) {
	Gdiplus::Image* pImageNormal = (Gdiplus::Image*)GetMember(L"pImageNormal");
	if (pImageNormal != nullptr) {
		delete pImageNormal;
	}
	if (参数_图片) {
		SetMember(L"pImageNormal", (LONG_PTR)参数_图片);
		return true;
	}
	return false;
}
ARGB DarkMoonUI::zyButtonEx::通常颜色(ARGB 参数_透明色ARGB) {
	if (参数_透明色ARGB == -1) {
		return (ARGB)GetMember(L"TransColorNormal");
	}
	else
	{
		SetMember(L"TransColorNormal", (LONG_PTR)参数_透明色ARGB);
		return 参数_透明色ARGB;
	}
}
bool DarkMoonUI::zyButtonEx::点燃图片(Gdiplus::Image* 参数_图片) {
	Gdiplus::Image* pImageHover = (Gdiplus::Image*)GetMember(L"pImageHover");
	if (pImageHover != nullptr) {
		delete pImageHover;
	}
	if (参数_图片) {
		SetMember(L"pImageHover", (LONG_PTR)参数_图片);
		return true;
	}
	return false;
}
ARGB DarkMoonUI::zyButtonEx::点燃颜色(ARGB 参数_透明色ARGB) {
	if (参数_透明色ARGB == -1) {
		return (ARGB)GetMember(L"TransColorHover");
	}
	else
	{
		SetMember(L"TransColorHover", (LONG_PTR)参数_透明色ARGB);
		return 参数_透明色ARGB;
	}
}
bool DarkMoonUI::zyButtonEx::按下图片(Gdiplus::Image* 参数_图片) {
	Gdiplus::Image* pImagePress = (Gdiplus::Image*)GetMember(L"pImagePress");
	if (pImagePress != nullptr) {
		delete pImagePress;
	}
	if (参数_图片) {
		SetMember(L"pImagePress", (LONG_PTR)参数_图片);
		return true;
	}
	return false;
}
ARGB DarkMoonUI::zyButtonEx::按下颜色(ARGB 参数_透明色ARGB) {
	if (参数_透明色ARGB == -1) {
		return (ARGB)GetMember(L"TransColorPress");
	}
	else
	{
		SetMember(L"TransColorPress", (LONG_PTR)参数_透明色ARGB);
		return 参数_透明色ARGB;
	}
}
bool DarkMoonUI::zyButtonEx::禁止图片(Gdiplus::Image* 参数_图片) {
	Gdiplus::Image* pImageDisabled = (Gdiplus::Image*)GetMember(L"pImageDisabled");
	if (pImageDisabled != nullptr) {
		delete pImageDisabled;
	}
	if (参数_图片) {
		SetMember(L"pImageDisabled", (LONG_PTR)参数_图片);
		return true;
	}
	return false;
}
ARGB DarkMoonUI::zyButtonEx::禁止颜色(ARGB 参数_透明色ARGB) {
	if (参数_透明色ARGB == -1) {
		return (ARGB)GetMember(L"TransColorDisabled");
	}
	else
	{
		SetMember(L"TransColorDisabled", (LONG_PTR)参数_透明色ARGB);
		return 参数_透明色ARGB;
	}
}
bool DarkMoonUI::zyButtonEx::焦点图片(Gdiplus::Image* 参数_图片) {
	Gdiplus::Image* pImageDefault = (Gdiplus::Image*)GetMember(L"pImageDefault");
	if (pImageDefault != nullptr) {
		delete pImageDefault;
	}
	if (参数_图片) {
		SetMember(L"pImageDefault", (LONG_PTR)参数_图片);
		return true;
	}
	return false;
}
ARGB DarkMoonUI::zyButtonEx::焦点颜色(ARGB 参数_透明色ARGB) {
	if (参数_透明色ARGB == -1) {
		return (ARGB)GetMember(L"TransColorDefault");
	}
	else
	{
		SetMember(L"TransColorDefault", (LONG_PTR)参数_透明色ARGB);
		return 参数_透明色ARGB;
	}
}
bool DarkMoonUI::zyButtonEx::图标(Gdiplus::Image* 参数_图片) {
	Gdiplus::Image* pIconImage = (Gdiplus::Image*)GetMember(L"pIconImage");
	if (pIconImage != nullptr) {
		delete pIconImage;
	}
	if (参数_图片) {
		SetMember(L"pIconImage", (LONG_PTR)参数_图片);
		return true;
	}
	return false;
}
SIZE DarkMoonUI::zyButtonEx::图标尺寸(SIZE& 参数_尺寸) {
	SIZE size = { (int)GetMember(L"IconWidth"),(int)GetMember(L"IconHeight") };
	
	if (参数_尺寸.cx==0) {
		SetMember(L"IconWidth", (LONG_PTR)参数_尺寸.cx);
		SetMember(L"IconWidth", (LONG_PTR)参数_尺寸.cy);
	}
	return size;
}
int DarkMoonUI::zyButtonEx::图标左偏移(int 参数_图标左偏移) {
	int IconMarginLeft =  (int)GetMember(L"IconMarginLeft");
	if (参数_图标左偏移) {
		SetMember(L"IconMarginLeft", 参数_图标左偏移);

	}
	return IconMarginLeft;
}
int DarkMoonUI::zyButtonEx::图标上偏移(int 参数_图标上偏移) {
	int IconMarginTop = (int)GetMember(L"IconMarginTop");
	if (参数_图标上偏移) {
		SetMember(L"IconMarginTop", 参数_图标上偏移);

	}
	return IconMarginTop;
}
int DarkMoonUI::zyButtonEx::图标右偏移(int 参数_图标右偏移) {
	int IconMarginRight = (int)GetMember(L"IconMarginRight");
	if (参数_图标右偏移) {
		SetMember(L"IconMarginRight", 参数_图标右偏移);

	}
	return IconMarginRight;
}
int DarkMoonUI::zyButtonEx::图标下偏移(int 参数_图标下偏移) {
	int IconMarginBottom = (int)GetMember(L"IconMarginBottom");
	if (参数_图标下偏移) {
		SetMember(L"IconMarginBottom", 参数_图标下偏移);

	}
	return IconMarginBottom;
}
int DarkMoonUI::zyButtonEx::图标对齐方式(int 参数_图标对齐方式) {
	int AlignIcon = (int)GetMember(L"AlignIcon");
	if (参数_图标对齐方式) {
		SetMember(L"AlignIcon", 参数_图标对齐方式);

	}
	return AlignIcon;
}
int DarkMoonUI::zyButtonEx::文本对齐方式(int 参数_文本对齐方式) {
	int AlignText = (int)GetMember(L"AlignText");
	if (参数_文本对齐方式) {
		SetMember(L"AlignText", 参数_文本对齐方式);

	}
	return AlignText;
}



void DarkMoonUI::zyButtonEx::置图片九宫(int 参数_左边, int 参数_顶边, int 参数_右边, int 参数_底边) {//调用了此方法后，图片按九宫方式绘制
	if (参数_左边 == 0) {
		SetMember(L"IsNineDraw", 0);
		return;
	}
	if (参数_顶边 == -1) {
		参数_顶边 = 参数_左边;
	}
	if (参数_右边 == -1) {
		参数_右边 = 参数_左边;
	}
	if (参数_底边 == -1) {
		参数_底边 = 参数_左边;
	}
	SetMember(L"IsNineDraw", 1);
	SetMember(L"NineLeft", 参数_左边);
	SetMember(L"NineTop", 参数_顶边);
	SetMember(L"NineRight", 参数_右边);
	SetMember(L"NineBottom", 参数_底边);
}